\defmodule {PascalConvolutionGen}

Implements \emph{Pascal} random variate generators by
the \emph{convolution} method\latex{ (see \cite{sLAW00a})}.
The method generates $n$ geometric variates with probability $p$
and adds them up.

The algorithm is slow if $n$ is large.
% A local copy of the parameters $n$ and $p$ is maintained in this class.


\bigskip\hrule


\begin{code}
\begin{hide}
/*
 * Class:        PascalConvolutionGen
 * Description:  Pascal random variate generators using the convolution method
 * Environment:  Java
 * Software:     SSJ 
 * Copyright (C) 2001  Pierre L'Ecuyer and Universite de Montreal
 * Organization: DIRO, Universite de Montreal
 * @author       
 * @since

 * SSJ is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License (GPL) as published by the
 * Free Software Foundation, either version 3 of the License, or
 * any later version.

 * SSJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * A copy of the GNU General Public License is available at
   <a href="http://www.gnu.org/licenses">GPL licence site</a>.
 */
\end{hide}
package umontreal.iro.lecuyer.randvar;\begin{hide}
import umontreal.iro.lecuyer.rng.*;
import umontreal.iro.lecuyer.probdist.*;
\end{hide}

public class PascalConvolutionGen extends PascalGen \begin{hide} {
\end{hide}
\end{code}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection* {Constructors}
\begin{code}

   public PascalConvolutionGen (RandomStream s, int n, double p)\begin{hide} {
      super (s, null);
      setParams (n, p);
   }\end{hide}
\end{code}
  \begin{tabb} Creates a \emph{Pascal} random variate generator with parameters
  $n$ and $p$, using stream \texttt{s}.
 \end{tabb}
\begin{code}

   public PascalConvolutionGen (RandomStream s, PascalDist dist)\begin{hide} {
      super (s, dist);
   }\end{hide}
\end{code}
  \begin{tabb}
  Creates a new generator for the distribution \texttt{dist}, using
  stream \texttt{s}.
 \end{tabb}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%\subsubsection* {Methods}
\begin{code}\begin{hide} 
    
   public int nextInt() {
      int x = 0;
      for (int i = 0; i < n; i++)
         x += GeometricDist.inverseF (p, stream.nextDouble());
      return x;

   }
    
   public static int nextInt (RandomStream s, int n, double p) {
     return convolution (s, n, p);
   }
\end{code}
 \begin{tabb} 
 Generates a new variate from the \emph{Pascal} distribution,
  with parameters $n = $~\texttt{n} and $p = $~\texttt{p}, using the stream \texttt{s}.
 \end{tabb}
\begin{code}

   private static int convolution (RandomStream stream, int n, double p) {
      int x = 0;
      for (int i = 0; i < n; i++)
         x += GeometricDist.inverseF (p, stream.nextDouble());
      return x;
   }
}\end{hide}
\end{code}
